package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.OrdersDTO;
import com.woniuxy.mesboot.dto.OrdersDetailsDTO;
import com.woniuxy.mesboot.dto.TicketsDetailsDTO;
import com.woniuxy.mesboot.entity.MesOrders;
import com.woniuxy.mesboot.service.OrderService;
import com.woniuxy.mesboot.service.OrdersdetailService;
import com.woniuxy.mesboot.service.TicketsDetailService;
import com.woniuxy.mesboot.service.WorkTicketsService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.OrdersDetailsVO;
import com.woniuxy.mesboot.vo.OrdersTicketVO;
import com.woniuxy.mesboot.vo.OrdersVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 黑川
 * @version : 1.0
 * @Project : mes-boot
 * @Package : com.woniuxy.mesboot.controller
 * @ClassName : .java
 * @createTime : 2023/5/23 17:57
 * @Description :生产订单表
 */
@RestController
@RequestMapping("/orders")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrdersdetailService ordersdetailService;
    @Resource
    private WorkTicketsService ticketsService;
    @Resource
    private TicketsDetailService detailService;

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody OrdersDTO orders){
        PageHelper.startPage(orders.getCurrentPage(),orders.getPageSize(),true);
        List<OrdersVO> list = orderService.selectAll(orders);
        PageInfo<OrdersVO> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",list).data("total",pageInfo.getTotal());
    }
    @GetMapping("allDetails/{no}")
    public Result queryAllDetails(@PathVariable String no){
        List<OrdersDetailsVO> detailsVOS = orderService.selectAllByDetails(no);
        return Result.ok().data("detailsVOS",detailsVOS);
    }
    @GetMapping("orderTicket/{ordersNo}")
    public Result queryOrderTicket(@PathVariable("ordersNo") String no){
        OrdersTicketVO ordersTicketVOS = orderService.selectOrderTickets(no);
        return Result.ok().data("ordersTicketVOS",ordersTicketVOS);
    }
    @PostMapping("tickets")
    public Result addTickets(@RequestBody TicketsDetailsDTO detailsDTO){
        int i = ticketsService.insertSelective(detailsDTO);
        if (i > 0) {
            int id = ticketsService.selectIdByNo(detailsDTO.getTicketNo());
            detailsDTO.setTicketId(id);
            detailService.insertSelective(detailsDTO);
            return Result.ok("转化成功");
        }else {
            return Result.ok("转化失败");
        }
    }

    @PostMapping("ordersdetails")
    public Result addDetails(@RequestBody OrdersDetailsDTO detailsDTO){
        orderService.insertSelective(detailsDTO);
        int i = ordersdetailService.insertSelective(detailsDTO);
        if(i>0){
            return Result.ok("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @GetMapping("{ordersNo}")
    public Result queryById(@PathVariable String ordersNo){
        MesOrders orders = orderService.selectByPrimaryKey(ordersNo);
        return Result.ok().data("orders",orders);
    }
    @PutMapping
    public Result update(@RequestBody MesOrders orders){
        int i = orderService.updateByPrimaryKeySelective(orders);
        if(i>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @DeleteMapping("{ordersNo}")
    public Result delete(@PathVariable("ordersNo")String ordersNo){
        int i = orderService.deleteByPrimaryKey(ordersNo);
        if(i>0){
            return Result.ok("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
